package com.example.initializer;

import com.example.kafka.EventHandlerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class KafkaListenerInitializer {

    private static final Logger logger = LoggerFactory.getLogger(KafkaListenerInitializer.class);
    private final EventHandlerFactory eventHandlerFactory;
    public List<String> topics = new ArrayList<>();

    public KafkaListenerInitializer(EventHandlerFactory eventHandlerFactory) {
        this.eventHandlerFactory = eventHandlerFactory;
    }

    @PostConstruct
    public void init() {
        // 计算并存储 topics 列表
        topics = eventHandlerFactory.getAllEventTypes()
                .stream()
                .map(Enum::name)
                .collect(Collectors.toList());
        logger.info("Initialized Kafka topics: {}", topics);
    }
}

